
//判断是否是对象
export function isObject(obj) {
  return Object.prototype.toString.call(obj) == '[object Object]';
}

//瀑布流计算
export function waterfallCalc(argArr) {
  let heightArr = [];
  const newArr = argArr.reduce(
    ({ right, left }, next) => {
      const lHeight = left.reduce((total = 0, item) => {
        return total + (item.height || 0)
      }, 0);
      const rHeight = right.reduce((total = 0, item) => {
        return total + (item.height || 0)
      }, 0);
      console.log(lHeight, rHeight);
      heightArr.push(lHeight)
      if (rHeight <= lHeight) {
        heightArr.push(lHeight)
        return {
          left,
          right: [...right, next]
        };
      } else {
        heightArr.push(rHeight)
        return {
          right,
          left: [...left, next]
        };
      }
    },
    { right: [], left: [] }
  );
  let arr = [newArr.right, newArr.left];
  return { arr, maxHeight: Math.max(...heightArr) }
}

//校验字符串是否为空
export function isNull(str) {
  return ['', null, undefined].includes(str)
}

// 防抖
export const debounce = (fn, delay = 500) => {
  let timer;
  console.log('----------------->', timer);
  return function () {
    let args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      timer = null;
      fn.apply(this, args);
    }, delay);
  };
};

//数字中文
export const toChineseNum = (num) => {
  const chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
  return chineseNums[num];
}

/**
 * 校验字符串是否包含 非中文 数字 和字母
 * @param {需要校验的字符串} str 
 * @returns 
 */
export function nonChineseAlphanumericCharacters(str) {
  return /[^\u4e00-\u9fa5\w]/.test(str);
}

// 是否是视频
export function isVideo(url) {
  if (!url) {
    return false;
  }
  const arr = url.split('.');
  return !['png', 'jpg', 'jpeg'].includes(arr[arr.length - 1]);
}



/**
 * 保存文件
 */
export function saveFile(url, fileType = 'image') {
  uni.showLoading({title: '正在保存图片...', mask: true})
  uni.downloadFile({
    url,
    success: result => {
      if (result.statusCode !== 200) return;
      const tempFilePath = result.tempFilePath;
      switch (fileType) {
        case 'image':
          saveImageToAlbum(tempFilePath);
          break;
        default:
          break;
      }
    },
    complete: e => {
      uni.hideLoading();
    }
  })
}

/**
 * 保存图片到相册
 * @param {downloadFile的临时路劲} tempFilePath 
 */
function saveImageToAlbum(tempFilePath) {
  uni.saveImageToPhotosAlbum({
    filePath: tempFilePath,
    success: res => {
      uni.showToast({ title: '保存成功', icon: 'none' })
    }, fail: err => {
      console.log('图片保存错误===>', err);
      uni.showToast({ title: '图片保存失败', icon: 'none' })
    }
  })
}

/**
 * 简化/ 转换数字
 * @param {需要处理的数字} num 
 * @param {除数} divisor 
 * @param {简化之后的单位} unit 
 * @returns 
 */
export function simplifyNum(num, divisor = 10000, unit = 'w') {
  if (num > unit) {
    return `${Math.ceil(num / divisor)}${unit}`;
  }
  return num;
}

/**
 * 校验手机号
 */
export function checkPhoneNumber(str) {
  let reg = /^1[3456789]\d{9}$/;
  return reg.test(str)
}